//
//  VisitorLoginView.swift
//  MicroBlog
//
//  Created by QT on 15/7/28.
//  Copyright © 2015年 綦敦涛. All rights reserved.
//

import UIKit
/**
*  访客视图
*/
protocol VisitorLoginViewDelegate : NSObjectProtocol{
    // 用户登录
    func visitorLoginViewWillLogin()
    // 用户注册
    func visitorLoginViewWillRegister()
}

class VisitorLoginView: UIView {
    
    weak var delegate: VisitorLoginViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame:frame)
        
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        
        setupUI()
    }
    
    /**
    供外部调用,用于设置访客视图的内容，根据当前的控制器（是否是Home）设置不同的显示内容，动画等
    
    - parameter isHome : 是否是Home控制器
    - parameter message: 当前要显示的信息
    - parameter imgName: 当前视图要显示的图片的名字
    */
    
    func setUpInfo(isHome: Bool,message: String,imgName: String){
        messageLabel.text = message
        smallIcon.image = UIImage(named: imgName)
        
        if !isHome{
            smallHouse.hidden = true
            sendSubviewToBack(maskIconView)
        }else{
            setUpAni()
        }
        
    }

    
    private func setupUI(){
        backgroundColor = UIColor(white: 0.93, alpha: 1)
        // 添加子控件
        addSubview(smallIcon)
        addSubview(maskIconView)
        addSubview(smallHouse)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
    }
    
    
    
    /**
    布局子控件
    */
    override func layoutSubviews() {
        super.layoutSubviews()
        
        // 设置smallIcon约束,视图居中
        smallIcon.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: smallIcon, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: smallIcon, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        // 设置smallHouse约束，与smallIcon对齐
        smallHouse.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: smallHouse, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: smallIcon, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: smallHouse, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: smallIcon, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))

        // 设置文本，参照图标，水平居中，垂直位于smallIcon下20
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: smallIcon, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: smallIcon, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 20))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 224))

        // 设置注册按钮(设置与文本左下（16）对齐)，高35，宽100
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        // 设置登录按钮约束(设置与文本右下（16）对齐)，高35，宽100
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
//
//        // 遮罩约束
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[maskView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["maskView": maskIconView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[maskView]-200-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["maskView": maskIconView]))
//        
//        addConstraint(NSLayoutConstraint(item: maskIconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: smallIcon, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 16))
        
    }
    
    
    
    // MARK: - 懒加载页面元素
    // 小图标
    private lazy var smallIcon: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return iv
    }()
    
    // 小房子
    private lazy var smallHouse: UIImageView = {
        let ho = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return ho
    }()
    // 描述文字
    private lazy var messageLabel: UILabel = {
        let msg = UILabel()
        msg.text = "关注一些人，回这里看有什么惊喜"
        msg.font = UIFont.systemFontOfSize(14)
        msg.textColor = UIColor.grayColor()
        msg.textAlignment = NSTextAlignment.Center
        msg.numberOfLines = 0
        msg.sizeToFit()
        return msg
    }()
    // 注册按钮
    private lazy var registerButton: UIButton = {
        let reg = UIButton()
        
        
        reg.setBackgroundImage(self.getStretchableImg(name: "common_button_white_disable"), forState: UIControlState.Normal)
        reg.setTitle("注册", forState: UIControlState.Normal)
        reg.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        // 添加事件
        reg.addTarget(self, action: "clickRegisterBtn", forControlEvents: UIControlEvents.TouchUpInside)
        
        return reg
    }()
    // 登录按钮
    private lazy var loginButton: UIButton = {
    let log = UIButton()
//        let img = getStretchableImg("common_button_white_disable")
        log.setBackgroundImage(self.getStretchableImg(name: "common_button_white_disable"), forState: UIControlState.Normal)
        log.setTitle("登录", forState: UIControlState.Normal)
        log.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        // 添加按钮事件
        log.addTarget(self, action: "clickLoginBtn", forControlEvents: UIControlEvents.TouchUpInside)
        return log
    }()
    // 遮罩图片
    private lazy var maskIconView: UIImageView = {
        let mask = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        
        return mask
    }()
    // 按钮图片背景可拉伸
    private func getStretchableImg(name imgName: String) -> UIImage {
        var originalImg = UIImage(named: imgName)
        let size = originalImg?.size
        
        originalImg = originalImg?.resizableImageWithCapInsets(UIEdgeInsets(top: (size?.height)!*0.5, left: (size?.width)!*0.5, bottom: (size?.height)!*0.5, right: (size?.width)!*0.5))
        
        return originalImg!
        
    }
    
    
    // 添加动画
    /**
    重点： removedCompletion ---->
    */
    func setUpAni(){
        let ani = CABasicAnimation(keyPath: "transform.rotation")
        ani.duration = 5
        ani.toValue = 2 * M_PI
        ani.repeatCount = MAXFLOAT
        ani.removedOnCompletion = false
        smallIcon.layer.addAnimation(ani, forKey: nil)
    }
    
    // 按钮点击事件
    func clickLoginBtn(){
        delegate!.visitorLoginViewWillLogin()
    }
    
    func clickRegisterBtn(){
        delegate!.visitorLoginViewWillRegister()
    }

}
